﻿using System.Collections.Generic;
using System;

namespace point2bmp
{
    public partial class main
    {
        static void TestAStar()
        {
            Random r = new Random((int)DateTime.Now.Ticks);

            int test = 10;   // 测试次数。
            for (int i = 0; i < test; i++)
            {
                MyVector3 start = new MyVector3() { x = r.Next(-1000, 1000), y = r.Next(-1000, 1000) };
                MyVector3 end = new MyVector3() { x = r.Next(-1000, 1000), y = r.Next(-1000, 1000) };

                var startidx = MapToCell(start);
                var endidx = MapToCell(end);

                var ret = AStar(start, end);

                if (ret == null)
                {
                    Console.WriteLine("寻路查询失败！");
                    Console.WriteLine("p: " + startidx.x + "," + startidx.y);
                    Console.WriteLine("p: " + endidx.x + "," + endidx.y);
                }
                else
                {
                    Console.WriteLine("寻路查询成功:");
                    // 寻路成功。
                    foreach (var it in ret)
                    {
                        Console.WriteLine("p: " + it.x + "," + it.y);
                    }
                }
            }
        }
        static List<MyPoint> AStar(MyVector3 start, MyVector3 end)
        {
            var startidx = MapToCell(start);
            var endidx = MapToCell(end);
            
            // 可行走路线标记
            bool[] cellmap = new bool[cellcount * cellcount];
            for (int i = 0; i < cellmap.Length; i++)
            {
                cellmap[i] = cellMap[i % cellcount, i / cellcount] != null;
            }

            // 
            AStarPath astar = new AStarPath();
            astar.Init(cellmap, cellcount);
         
            var ret = astar.GetPath(startidx, endidx);
            return ret;
        }       
    }
}